import store from '../../westore/store'
import create from '../../westore/utils/create'

import Config from "../../config"
import Api from "../../utils/api"
import {
  Modal
} from "../../utils/common"
import deepCopy from "../../utils/common"
const app = getApp(),
  regeneratorRuntime = require("../../utils/regenerator-runtime/runtime")

create(store, {
  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    focus: false,
    searchState: false,
    pagenum:1,
    pages:1,
    theActivityData:[],
    inputValue: '',
    sort:"id"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    wx.showLoading({
      title: '加载中',
    })
    const _this = this;
    const openid = _this.store.data.openid;
    const pagenum = _this.data.pagenum;
    const sort_field = _this.data.sort;
    const res = await Api.getActivityePage(pagenum, "", "", sort_field);
    wx.hideLoading();
    if(res.status==0){
      _this.setData({
        theActivityData: res.data.items,
        pages: res.data.pages
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },

  goAddActivity: function (e) {
    const thetype = e.currentTarget.dataset.thetype
    const actid = e.currentTarget.dataset.actid
    // console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '/pages/addActivity/addActivity?thetype=' + thetype + '&theactid=' + actid
    })
  },
  showInput: function showInput() {
    this.setData({
      focus: true,
      searchState: true
    });
  },
  hideInput: function hideInput() {
    this.setData({
      focus: false,
      searchState: false
    });
  },
  input: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  search: async function(){
    const _this = this;
    const keyword = _this.data.inputValue;
    const openid = _this.store.data.openid;
    const sort_field = _this.data.sort;
    const pagenum = 1;
    _this.setData({pagenum})
    wx.showLoading({
      title: '请稍侯...',
    })
    const res = await Api.getActivityePage(pagenum, "", keyword,sort_field);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        theActivityData: res.data.items,
        pages:res.data.pages
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    const _this = this;
    const openid = _this.store.data.openid;
    let pagenum = _this.data.pagenum+1;
    const keyword = _this.data.inputValue;
    const sort_field = _this.data.sort;
    if(pagenum>_this.data.pages)return;
    wx.showLoading({
      title: '请稍侯...',
    })
    const res = await Api.getActivityePage(pagenum, "", keyword,sort_field);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        theActivityData: _this.data.theActivityData.concat(res.data.items),
        pagenum:pagenum
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },

  changesort: async function(){
    wx.showLoading({
      title: '请稍侯...',
    })
    const _this = this;
    const openid = _this.store.data.openid;
    let pagenum =  1;
    const keyword = _this.data.inputValue;
    const sort_field = _this.data.sort=="id"?"like":"id";
    const res = await Api.getActivityePage(pagenum, "", keyword, sort_field);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        theActivityData: res.data.items,
        pagenum: pagenum,
        sort: sort_field
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})